package kr.co.mcshop.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import kr.co.mcshop.order.OrderService;
import kr.co.mcshop.product.ProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

@RequestMapping(value = "/user")
@Controller
public class UserController {
   @Autowired
   UserService us;
   
   @Autowired
   OrderService os;
   
   @Autowired
   ProductService ps;
   
   @RequestMapping(value="/idCheck",  method = RequestMethod.POST, produces="text/plain;charset=UTF-8")
   @ResponseBody
   public String userIdCheck(@RequestParam Map map){
      map.put("user_id", map.get("message"));
      User userInfo = (User) us.UserLogin(map);
      Gson gson = new Gson();
      if(userInfo == null){   
         String str="사용가능한 아이디 입니다.";
         return gson.toJson(str);
      }else{
         String str="이미 사용중인 아이디 입니다.";
         return gson.toJson(str);
      }
   }
   
   @RequestMapping(value="/InsertForm")
   public String UserInsertForm(){
      return "userInsertForm";
   }
   
   @RequestMapping(value="/Insert",  method = RequestMethod.POST, produces="text/plain;charset=UTF-8")
   public String UserInsert(@RequestParam Map map){
      User userInfo = (User) us.UserLogin(map);
      Gson gson = new Gson();
      if(userInfo == null){   
         us.UserInsert(map);
         return "redirect:/";
      }else{
         return "userInsertForm";
      }
   
   }
   
   @RequestMapping(value="/UpdateForm")
   public String UpdateForm(@RequestParam Map map, Model model){
      List<User> user = (List<User>) us.UserList(map);
      model.addAttribute("no", user.get(0).getNo());
      return "UpdateForm";
   }
   
   @RequestMapping(value="/Update", method = RequestMethod.POST)
   public String UserInfoUpdate(@RequestParam Map map, HttpSession session){
      //System.out.println(map);
      us.UserUpdate(map);
      User user = us.UserLogin(map);
      session.setAttribute("loginUser", user);
      return "redirect:/user/Info";
   }

   @RequestMapping(value="/Delete")
   public String UserDelete(@RequestParam Map map,HttpSession session){
      us.UserDelete(map);
      session.removeAttribute("loginUser");
      return "redirect:/";
   }
   @RequestMapping(value="/Mypage")
   public String Mypage(@RequestParam Map map,Model model){
      System.out.println(map);
      List<User> user = (List<User>) us.UserList(map);
      model.addAttribute("member", user);
      return "Mypage";
   }
   @RequestMapping(value="/Info")	
   public String UserInfo(@RequestParam Map map,Model model){
      System.out.println(map);
      List<User> user = (List<User>) us.UserList(map);
      model.addAttribute("member", user);
      return "userInfo";
   }
   
   
   @RequestMapping(value="/login")
   public String Userlogin(){
      return "login";
   }
   
   @RequestMapping(value="/logOut")
   public String Userlogin(HttpSession session){
      session.removeAttribute("loginUser");
      session.removeAttribute("itemList");
      System.out.println("itemList" + session.getAttribute("itemList"));
      return "redirect:/";
   }
   @RequestMapping(value="/loginSuccess")
   public String UserloginSuccess(@RequestParam Map map, HttpSession session){
      User user = us.UserLogin(map);
      if(user==null){
         return "login";
      }else{
         session.setAttribute("loginUser", user);
      }
      session.setAttribute("loginUser", user);
      return "redirect:/";
   }
   
   @RequestMapping(value="/userOrder")
   public String userOrder(@RequestParam Map map,HttpSession session,Model model){
      User loginUser = (User) session.getAttribute("loginUser");
      List<Map> userOrder = os.getUserOrder(map);
      //현재 로그인한 유저의 no(기본키)값을 통하여, 유저의 no와 동일한 주문리스트를 가져옴.

      for(int i=0; i<userOrder.size(); i++){
         
         HashMap<Object,Object> hashMap = new HashMap<Object,Object>();
         hashMap.put("no", userOrder.get(i).get("product_no"));
         System.out.println(hashMap);
         List<Map> userOrderList = ps.getAllProduct(hashMap);
         //유저가 주문한 상품이 복수일 수도 있기 때문에, 유저가 주문한 상품에 대한 결과를
         //List<Map>형태로 받아옴
         System.out.println("1111" + userOrderList.get(0));
         String product_name = (String) userOrderList.get(0).get("NAME");
         String product_price = (String) userOrderList.get(0).get("PRICE").toString();
         String product_image = (String) userOrderList.get(0).get("IMAGE");
         String[] productMainImage = product_image.split("[/]");
         //각각 받아온 List<Map>형태에서 NAME,PRICE,IMAGE를 가져옴.
         
         userOrder.get(i).put("NAME", product_name);
         userOrder.get(i).put("PRICE", product_price);
         userOrder.get(i).put("image", productMainImage[0]);
         //받아온 이미지를 user
         System.out.println(userOrder);
      }
      System.out.println(userOrder);
      model.addAttribute("userOrder", userOrder);
      return "userOrder";
   }
   @RequestMapping(value="/userOrderCancel")
   public String userOrderCancel(@RequestParam Map map,HttpSession session,Model model){
      User loginUser = (User) session.getAttribute("loginUser");
      List<Map> userOrder = os.getUserOrder(map);
      //현재 로그인한 유저의 no(기본키)값을 통하여, 유저의 no와 동일한 주문리스트를 가져옴.

      for(int i=0; i<userOrder.size(); i++){
         
         HashMap<Object,Object> hashMap = new HashMap<Object,Object>();
         hashMap.put("no", userOrder.get(i).get("product_no"));
         System.out.println(hashMap);
         List<Map> userOrderList = ps.getAllProduct(hashMap);
         //유저가 주문한 상품이 복수일 수도 있기 때문에, 유저가 주문한 상품에 대한 결과를
         //List<Map>형태로 받아옴
         System.out.println("1111" + userOrderList.get(0));
         String product_name = (String) userOrderList.get(0).get("NAME");
         String product_price = (String) userOrderList.get(0).get("PRICE").toString();
         String product_image = (String) userOrderList.get(0).get("IMAGE");
         String[] productMainImage = product_image.split("[/]");
         //각각 받아온 List<Map>형태에서 NAME,PRICE,IMAGE를 가져옴.
         
         userOrder.get(i).put("NAME", product_name);
         userOrder.get(i).put("PRICE", product_price);
         userOrder.get(i).put("image", productMainImage[0]);
         //받아온 이미지를 user
         System.out.println(userOrder);
      }
      model.addAttribute("userOrder", userOrder);
      return "userOrderCancel";
   }
   @RequestMapping(value="/userOrderEdit")
   public String userOrderEdit(@RequestParam Map map){
      map.put("state", "주문취소");
      os.UpdateOrder(map);
      return "redirect:/";
   }
}